#include "Thread.h"
#include <iostream>

using std::cout;
using std::endl;

Thread::Thread(const TFunc& Func)
    :_pthID(0)
     ,_isRunning(false)
     ,_tCallBack(Func)
{
    cout << "Thread()" << endl;
}

Thread::~Thread()
{
	if (_isRunning)
	{
		 pthread_detach(_pthID);
   		 cout << "~Thread()" << endl;
	}
}

void Thread::start()
{
    if (_isRunning)
    {
        return ;
    }
    pthread_create(&_pthID, NULL, threadFunc, this);
    _isRunning = true;
}

void Thread::join()
{
    if (!_isRunning)
    {
        return ;
    }

    pthread_join(_pthID, NULL);
    _isRunning = false;
}

void* Thread::threadFunc(void* pArg)
{
    if (NULL == pArg)
    {
        return NULL;
    }

    Thread* thread = static_cast<Thread*>(pArg);
    thread->_tCallBack();

    return NULL;
}
